package com.sig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import utils.Polygon;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Recherche extends BaseAdapter{

	private LayoutInflater inflater;
	public ViewGroup layout;
	public ListView liste;
	private HashMap<String,Polygon> polygonesOriginal;
	private List<Polygon> polygonesRecherche = new ArrayList<Polygon>();
	private List<SpannableStringBuilder> spans = new ArrayList<SpannableStringBuilder>();

	public Recherche(Activity activity, ViewGroup layout, HashMap<String,Polygon> polygones){
		this.polygonesOriginal = polygones;
		this.layout = layout;
		inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View l = inflater.inflate(R.layout.liste_recherche, null);
		this.liste = (ListView)(l.findViewById(R.id.recherche_listview));		
		layout.addView(l);
		chercher("");
	}

	public void reset(){
		polygonesRecherche.clear();
		spans.clear();
		this.liste.setAdapter(null);
	}

	public utils.Polygon retourner(int poly){
		return this.polygonesRecherche.get(poly);
	}

	@SuppressLint("DefaultLocale")
	public void chercher(String texte){		
		reset();

		Pattern p = Pattern.compile(texte.toLowerCase().replace("�", "e").replace("�","a").replace("�","e"));

		for(Entry<String, Polygon> entry : polygonesOriginal.entrySet()){
			if(entry.getValue().getNom() != null && !entry.getValue().getNom().trim().isEmpty() && !entry.getValue().getNom().startsWith("zone") &&
					!entry.getValue().getType().trim().equalsIgnoreCase("trou")){
				String nom = entry.getValue().getNom();
				String nomComparable = nom.toLowerCase().replace("�", "e").replace("�","a").replace("�","e");
				final Matcher matcher = p.matcher(nomComparable);
				final SpannableStringBuilder spannable = new SpannableStringBuilder(nom);
				final ForegroundColorSpan span = new ForegroundColorSpan(Color.BLACK);
				if (matcher.find()) {
					spannable.setSpan(
							span, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
							);
					spans.add(spannable);
					polygonesRecherche.add(entry.getValue());
				}
			}
		}
		this.liste.setAdapter(this);
	}

	@Override
	public int getCount() {
		return polygonesRecherche.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View reuse, ViewGroup parent) {
		View cell = inflater.inflate(R.layout.liste_recherche_element, null);
		TextView tv = (TextView)cell.findViewById(R.id.recherche_element_texte);
		tv.setText(this.spans.get(position));
		return cell;
	}

}
